package com.aijk.ehm.sys.mq.handler;

import com.aijk.ehm.sys.api.LogApiImpl;
import com.aijk.ehm.sys.model.SysLog;
import com.alibaba.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import com.alibaba.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import com.alibaba.rocketmq.common.message.MessageExt;
import com.lifesea.rocketmq.utils.handler.MessageHandler;
import com.lifesea.rocketmq.utils.message.MessageDecoder;
import com.lifesea.rocketmq.utils.message.codec.JsonMessageDecoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;

/**
 * 日志操作消息处理器
 */
@Component
public class SysLogHandler implements MessageHandler<SysLog> {
    @Resource
    private LogApiImpl logApi;
    private static final Logger logger = LoggerFactory.getLogger(SysLogHandler.class);
    @Override
    public ConsumeConcurrentlyStatus process(SysLog log, MessageExt message, ConsumeConcurrentlyContext context) {
        logger.info("日志操作", message.getMsgId(), log);
        try {
                logApi.insertLog(log);
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        } catch (Exception e) {
            logger.error("日志处理失败", e);
        }
        return ConsumeConcurrentlyStatus.RECONSUME_LATER;
    }

    @Override
    public MessageDecoder<SysLog> getMessageDecoder() {
        return new JsonMessageDecoder<SysLog>(SysLog.class);
    }
}
